#include "HoldItem.h"

#include "../CommandManager.h"
#include "../GameObjects/Room.h"
#include "../GameObjects/Character.h"
#include "../GameObjects/Item.h"
#include "../Player.h"

#include <iostream>

void HoldItem::Init()
{
	CommandManager::getSingleton()->add(std::make_shared<Hold>());
	CommandManager::getSingleton()->add(std::make_shared<Wield>());
	CommandManager::getSingleton()->add(std::make_shared<Remove>());
	CommandManager::getSingleton()->add(std::make_shared<Sheath>());
}

///////////////////////////////////////
// HOLD ITEM
///////////////////////////////////////
Hold::Hold() : Command("hold")
{
}
void Hold::execute(const CharacterPtr &character, const RoomPtr &/*room*/, const std::vector<std::string> &args)
{
	if(args.empty())
	{
		if(character->hasPlayer())
			std::cout << "What do you want to hold?" << std::endl;
		return;
	}

	if(args.size() == 1)
	{
		for(unsigned int i = 0; i < character->getItems().size(); i++)
		{
			if(character->getItems()[i]->is(args[0]))
			{
				ItemPtr item = character->getItems()[i];
				if(character->hold(item))
				{
					character->remove(item);
					item->setStance(Item::IN_HAND);
				}
				return;
			}
		}
		if(character->hasPlayer())
			std::cout << "You don't have that." << std::endl;

	}
}

///////////////////////////////////////
// WIELD ITEM/WEAPON
///////////////////////////////////////
Wield::Wield() : Command("wield")
{
	add("wi");
}
void Wield::execute(const CharacterPtr &character, const RoomPtr &/*room*/, const std::vector<std::string> &args)
{
	if(args.empty())
	{
		if(character->hasPlayer())
			std::cout << "What do you want to wield?" << std::endl;
		return;
	}

	if(args.size() == 1)
	{
		for(unsigned int i = 0; i < character->getItems().size(); i++)
		{
			if(character->getItems()[i]->is(args[0]))
			{
				ItemPtr item = character->getItems()[i];
				if(character->wield(item))
				{
					character->remove(item);
					item->setStance(Item::IN_HAND);
				}
				return;
			}
		}
		if(character->hasPlayer())
			std::cout << "You don't have that." << std::endl;
	}
}

///////////////////////////////////////
// REMOVE  HELD ITEM
///////////////////////////////////////
Remove::Remove() : Command("remove")
{
}
void Remove::execute(const CharacterPtr &character, const RoomPtr &/*room*/, const std::vector<std::string> &args)
{
	if(args.empty())
	{
		ItemPtr item = character->removeItemInHand();
		if(item)
		{
			character->add(item);
			item->setStance(Item::IN_POCKET);
			if(character->hasPlayer())
				std::cout << "You put " << item->getIndefiniteArticle() << " " << item->getName() << " back in your pocket. " << std::endl;
			else
				std::cout << name << " puts " << item->getIndefiniteArticle() << " " << item->getName() << " back in his pocket." << std::endl;
		}
		else
		{
			std::cout << "Your hands are already empty!" << std::endl;
		}
	}
}

///////////////////////////////////////
// SHEATH  HELD ITEM
///////////////////////////////////////
Sheath::Sheath() : Command("sheath")
{
	add("sh");
}
void Sheath::execute(const CharacterPtr &character, const RoomPtr &/*room*/, const std::vector<std::string> &args)
{
	if(args.empty())
	{
		ItemPtr item = character->removeItemInHand();
		if(item)
		{
			character->add(item);
			item->setStance(Item::IN_POCKET);
			if(character->hasPlayer())
				std::cout << "You sheath " << item->getIndefiniteArticle() << " " << item->getName() << ". " << std::endl;
			else
				std::cout << name << " sheaths " << item->getIndefiniteArticle() << " " << item->getName() << "." << std::endl;
		}
		else
		{
			std::cout << "Your hands are already empty!" << std::endl;
		}
	}
}

